package com.qf.controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 文件上传
 */

@WebServlet("/upload")
@MultipartConfig
public class UploadFileServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //接收其他的表单数据
        String pname = request.getParameter("pname");
        System.out.println(pname) ;

        //1)通过请求对象Part对象----它可以解析文件名称
        Part part = request.getPart("file") ;//参数--->文件上传组件的name属性值
        //2)获取上传文件的名称
        String fileName = part.getSubmittedFileName() ;
        System.out.println("上传文件的名称是:"+fileName) ;
        //3)获取文件的存储路径---项目部署路径
        String realPath = request.getServletContext().getRealPath("/myImage");
        System.out.println("文件存储路径是:"+realPath) ;

        // D:\EE_2302\day43\code\Maven_2302\src\main\webapp\myImage
        //4)通过part对象将写入磁盘上--->realpath地址里面
        part.write(realPath+"/"+fileName);
      //  part.write("") ; //绝对路径


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
